package br.ufrj.ppgi.jemf_advanced_example.text_message;

import java.util.Date;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.TextMessage;
import br.ufrj.ppgi.jemf.mobile.view.TextMessageView;
import br.ufrj.ppgi.jemf.utils.DateTimeFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single TextMessage detail screen.
 * This fragment is either contained in a {@link TextMessageListActivity} in two-pane mode (on tablets) or a {@link TextMessageDetailActivity} on handsets.
 */
public class TextMessageDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_TEXTMESSAGE = "activated_text_message";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "TEXTMESSAGE";
    /**
     * The TextMessage content this fragment is showing.
     */
    private static TextMessage textMessage;
    /**
     * The TextMessage view holder.
     */
    private static TextMessageView textMessageView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public TextMessageDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the TextMessage content specified by the fragment arguments.
        if (getArguments().containsKey(TextMessageDetailFragment.ARG_ID)) {
        	textMessage = getArguments().getParcelable(TextMessageDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the TextMessage content
	    if (textMessage != null) {
	    	textMessageView = new TextMessageView(getActivity(), false);
	    	// Content
	    	textMessageView.setTextMessageContentText(textMessage.getContent());
	    	// Priority
	    	textMessageView.setTextMessagePriorityText(textMessage.getPriorityString());
	    	// Creation Date
	    	textMessageView.setTextMessageCreationDateText(textMessage.getCreationDateString());
	    	// Last Modification Date
	    	textMessageView.setTextMessageLastModificationDateText(textMessage.getLastModificationDateString());
	    	// Subject
	    	textMessageView.setTextMessageSubjectText(textMessage.getSubject());

	    	// Its necessary to add the TextMessageView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add TextMessage View as a child of the ScrollView.
	    	scrollView.addView(textMessageView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated TextMessage
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_TEXTMESSAGE)) {
        	textMessage = savedInstanceState.getParcelable(STATE_TEXTMESSAGE);
        }
	}





	/**
     * Get updated TextMessage
     * @return TextMessage
     */
	public static TextMessage getTextMessage() {
		if (BuildConfig.DEBUG) Log.i(TextMessageListActivity.TAG, "TextMessageDetailFragment ::: getTextMessage");
    	return textMessage;
    }

	/**
	 * Update TextMessage data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((textMessage != null) && (textMessageView != null)) {
    		if (BuildConfig.DEBUG) Log.i(TextMessageListActivity.TAG, "TextMessageDetailFragment ::: updateDataFromUI");
    		// Update TextMessage object with new data included by user through components
    		textMessage.setContent(textMessageView.getTextMessageContentText());
    		textMessage.setPriorityString(textMessageView.getTextMessagePriorityText());
    		textMessage.setCreationDateString(textMessageView.getTextMessageCreationDateText());
			Date now = new Date(System.currentTimeMillis());
			textMessageView.setTextMessageLastModificationDateText(DateTimeFormat.DateTimeToString(now));
    		textMessage.setLastModificationDateString(textMessageView.getTextMessageLastModificationDateText());
    		textMessage.setSubject(textMessageView.getTextMessageSubjectText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated TextMessage.
        outState.putParcelable(STATE_TEXTMESSAGE, textMessage);
    }
}
